package com.samebuilding.service.impl;

import java.util.List;

import org.springframework.transaction.annotation.Transactional;

import com.samebuilding.dao.CommentDAO;
import com.samebuilding.dao.CommentReplyDAO;
import com.samebuilding.domain.Comment;
import com.samebuilding.domain.CommentReply;
import com.samebuilding.service.CommentService;

public class CommentServiceImpl implements CommentService {

	private CommentDAO commentDAO;
	private CommentReplyDAO commentReplyDAO;
	
	
	public CommentDAO getCommentDAO() {
		return commentDAO;
	}

	public void setCommentDAO(CommentDAO commentDAO) {
		this.commentDAO = commentDAO;
	}

	public CommentReplyDAO getCommentReplyDAO() {
		return commentReplyDAO;
	}

	public void setCommentReplyDAO(CommentReplyDAO commentReplyDAO) {
		this.commentReplyDAO = commentReplyDAO;
	}

	@Transactional
	public void addComment(Comment omment) {
		this.commentDAO.save(omment);

	}

	
	@Transactional
	public void addCommentReply(CommentReply commentReply) {
		this.commentReplyDAO.save(commentReply);

	}


	@Transactional
	public void deleteById(Integer id) {
		this.commentDAO.delete(id);
		this.commentReplyDAO.deleteCommentReplyByCommentId(id);

	}

	@Transactional
	public void deleteByIdAndReceiveUserNo(Integer id, Integer receiveUserNo) {
		this.commentReplyDAO.deleteCommentReplyByCommentId(id);
		this.commentDAO.deleteByIdAndReceiveUserNo(id, receiveUserNo);
		

	}

	@Transactional
	public void deleteByIdAndSendUserNo(Integer id, Integer sendUserNo) {
		this.commentReplyDAO.deleteCommentReplyByCommentId(id);
		this.commentDAO.deleteByIdAndSendUserNo(id, sendUserNo);
		

	}

	
	public List<Comment> findAll(int start, int limit) {
		
		return this.commentDAO.findAll(start, limit);
	}

	public int findCountAll() {
		return (int)this.commentDAO.getTotalCount();
	}

	public int getCountISendComment(Integer sendUserNo) {
		
		return this.commentDAO.getCountAllCommentISend(sendUserNo);
	}

	public int getCountISendCommentByType(Integer sendUserNo, Integer type) {
		
		return this.commentDAO.getCountCommentISendByType(sendUserNo, type);
	}

	public int getCountIReceiveComment(Integer receiveUserNo) {
		
		return this.commentDAO.getCountAllCommentIReceive(receiveUserNo);
	}

	public int getCountIReceiveCommentByType(Integer receiveUserNo, Integer type) {
		
		return this.commentDAO.getCountCommentIReceiveByType(receiveUserNo, type);
	}

	public int getCountUnreadCommentReply(Integer sendUserNo) {
		
		return this.commentReplyDAO.getCountUnreadCommentReply(sendUserNo);
	}

	public int getCountUnreadReceiveComment(Integer receiveUserNo) {
		
		return this.commentDAO.getCountUnreadComment(receiveUserNo) + this.commentReplyDAO.getCountUnreadComment(receiveUserNo);
	}

	public List<Comment> getISendComment(Integer sendUserNo, int start,
			int limit) {
		
		return this.commentDAO.getAllCommentISend(sendUserNo, start, limit);
	}

	public List<Comment> getISendCommentByType(Integer sendUserNo,
			Integer type, int start, int limit) {
		
		return this.commentDAO.getCommentISendByType(sendUserNo, type, start, limit);
	}

	public List<Comment> getIReceiveComment(Integer receiveUserNo, int start,
			int limit) {
		
		return this.commentDAO.getAllCommentIReceive(receiveUserNo, start, limit);
	}

	public List<Comment> getIReceiveCommentByType(Integer receiveUserNo,
			Integer type, int start, int limit) {
		
		return this.commentDAO.getCommentIReceiveByType(receiveUserNo, type, start, limit);
	}

	public List<CommentReply> getReplyByCommentId(Integer commentId) {
		//TODO 
		return null;
	}

	@Transactional
	public void updateCommentReplyUnreadToRead(Integer sendUserNo) {
		this.commentReplyDAO.updateCommentReplyUnreadToRead(sendUserNo);

	}

	@Transactional
	public void updateCommentUnreadToRead(Integer receiveUserNo) {
		this.commentDAO.updateUnreadToRead(receiveUserNo);
		this.commentReplyDAO.updateCommentUnreadToRead(receiveUserNo);

	}

	public List<Comment> getCommentByType(Integer foreignId, Integer type, int start, int limit) {
		
		return this.commentDAO.getCommentByType(foreignId, type, start, limit);
	}

	public int getCountCommentByType(Integer foreignId, Integer type) {
		
		return this.commentDAO.getCountCommentByType(foreignId, type);
	}

}
